package com.lanlian.cayman.view;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.cayman.activity.MessageDetailActivity;
import com.lanlian.commonlib.bean.MessageInfo;
import com.lanlian.commonlib.bean.db.DBMessage;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/9/14 16:39
 *     desc   : todo
 *     version: 1.0
 * </pre>
 */
public class MessageDialog extends FragmentActivity {

    private MessageInfo mMessageInfo;
    private TextView mTvTitle;
    private ImageView mImgClose;
    private WebView mWebView;
    private TextView mTvDetail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_message);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        initData();
        initView();
        setMessageInfo();
        mTvDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MessageDialog.this, MessageDetailActivity.class)
                        .putExtra("messageInfo", new DBMessage(mMessageInfo)));
                finish();
            }
        });
        mImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView() {
        mTvTitle = findViewById(R.id.tv_title);
        mImgClose = findViewById(R.id.img_close);
        mWebView = findViewById(R.id.tv_content);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new MyWebViewClient());
        mTvDetail = findViewById(R.id.tv_detail);
        mTvDetail.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        mMessageInfo = (MessageInfo) intent.getSerializableExtra("messageInfo");
        setMessageInfo();
    }

    private void initData() {
        mMessageInfo = (MessageInfo) getIntent().getSerializableExtra("messageInfo");
    }

    private void setMessageInfo() {
        mTvTitle.setText(mMessageInfo.getTitle());
        mWebView.loadDataWithBaseURL(null, mMessageInfo.getContent(), "text/html", "UTF-8", null);
    }

    private class MyWebViewClient extends WebViewClient {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            imgReset();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    /**
     * 对图片进行重置大小，宽度就是手机屏幕宽度，高度根据宽度比便自动缩放
     **/
    private void imgReset() {
        if (mWebView != null) {
            mWebView.loadUrl("javascript:(function(){" +
                    "var objs = document.getElementsByTagName('img'); " +
                    "for(var i=0;i<objs.length;i++)  " +
                    "{"
                    + "var img = objs[i];   " +
                    "    img.style.maxWidth = '100%'; img.style.height = 'auto';  " +
                    "}" +
                    "})()");
        }
    }

    @Override
    protected void onDestroy() {
        if (mWebView != null) {
            mWebView = null;
        }
        super.onDestroy();
    }
}
